local test = UnitTest:New("05DonglePrint")
local Dongle = DongleStub("Dongle-1.0")

function test:dongle_enable_debug()
	assert_fails(function() Dongle.EnableDebug({}) end, "You must call 'EnableDebug' from a registered Dongle.")
	local pi = Dongle:New("Pi")
	assert_fails(function() Dongle:EnableDebug(true) end, argerror(2))
end

function test:dongle_print()
	assert_fails(function() Dongle.Print({}) end, "You must call 'Print' from a registered Dongle.")
	assert_fails(function() Dongle.PrintF({}) end, "You must call 'PrintF' from a registered Dongle.")
	local rho = Dongle:New("Rho")

	local cf = rawget(getmetatable(getfenv(0)).__index, "ChatFrame1")
	cf.old = cf.AddMessage
	local last
	cf.AddMessage = function(self, msg, ...)
		last = msg
		--cf.old(cf, msg, ...)
	end

	rho:Print("This is a test print")
	assert(last == "|cFF33FF99Rho|r: This is a test print")
	rho:Print("This is a test print", "It has many args")
	assert(last == "|cFF33FF99Rho|r: This is a test print, It has many args")
	assert_fails(function() rho:PrintF("invalid format %d", "test") end, argerror(3), "'PrintF'")
	assert_fails(function() rho:PrintF("invalid format %d") end, argerror(3), "'PrintF'")

	cf.AddMessage = nil
end

function test:dongle_echo()
	assert_fails(function() Dongle.Echo({}) end, "You must call 'Echo' from a registered Dongle.")
	assert_fails(function() Dongle.EchoF({}) end, "You must call 'EchoF' from a registered Dongle.")
	local rho = Dongle:New("EchoTestRho")

	local cf = rawget(getmetatable(getfenv(0)).__index, "ChatFrame1")
	cf.old = cf.AddMessage
	local last
	cf.AddMessage = function(self, msg, ...)
		last = msg
		--cf.old(cf, msg, ...)
	end

	rho:Echo("This is a test echo")
	assert(last == "This is a test echo")
	rho:Echo("This is a test echo", "It has many args")
	assert(last == "This is a test echo, It has many args")
	assert_fails(function() rho:EchoF("invalid format %d", "test") end, argerror(2), "'EchoF'")
	assert_fails(function() rho:EchoF("invalid format %d") end, argerror(2), "'EchoF'")

	cf.AddMessage = nil
end

function test:dongle_debug()
	assert_fails(function() Dongle.Debug({}) end, "You must call 'Debug' from a registered Dongle.")
	local sigma = Dongle:New("Sigma")
	assert_fails(function() sigma:Debug("Test") end, argerror(2))
	assert_fails(function() sigma:DebugF("invalid format %d", "test") end, argerror(2))

	local cf = rawget(getmetatable(getfenv(0)).__index, "ChatFrame1")
	cf.old = cf.AddMessage
	local last
	cf.AddMessage = function(self, msg, ...)
		last = msg
		--cf.old(cf, msg, ...)
	end

	sigma:Debug(1, "This is a test debug")
	assert(last == nil)
	sigma:EnableDebug(1)
	sigma:Debug(2, "This should not be visible")
	assert(last == nil)
	sigma:Debug(1, "This should go through")
	assert(last == "|cFF33FF99Sigma|r: This should go through")
	sigma:Debug(0, "This is a higher debug")
	assert(last == "|cFF33FF99Sigma|r: This is a higher debug")
	cf.AddMessage = nil
end

test:Run()